/*
* Copyright 2024 NXP
* NXP Confidential and Proprietary. This software is owned or controlled by NXP and may only be used strictly in
* accordance with the applicable license terms. By expressly accepting such terms or by downloading, installing,
* activating and/or otherwise using the software, you are agreeing that you have read, and that you agree to
* comply with and are bound by, such license terms.  If you do not agree to be bound by the applicable license
* terms, then you may not retain, install, activate or otherwise use the software.
*/

#include "events_init.h"
#include <stdio.h>
#include "lvgl.h"

#if LV_USE_FREEMASTER
#include "freemaster_client.h"
#endif


lv_timer_t * task1 = NULL;

char flag = 0;

void task1_cb(lv_timer_t * tmr)
{
  if(flag)
  {
    lv_led_set_brightness(guider_ui.screen_3_led_1,0);
    flag = 0;
	printf("LED_OFF\n");
  }
  else
  {
    lv_led_set_brightness(guider_ui.screen_3_led_1,255);
    flag = 1;
	printf("LED_ON\n");
  }
}
int slider_value;
static void screen_btn_2_event_handler (lv_event_t *e)
{
	lv_event_code_t code = lv_event_get_code(e);

	switch (code) {
	case LV_EVENT_CLICKED:
	{
		ui_load_scr_animation(&guider_ui, &guider_ui.screen_1, guider_ui.screen_1_del, &guider_ui.screen_del, setup_scr_screen_1, LV_SCR_LOAD_ANIM_OVER_LEFT, 200, 10, true, true);
		break;
	}
	default:
		break;
	}
}
static void screen_btn_3_event_handler (lv_event_t *e)
{
	lv_event_code_t code = lv_event_get_code(e);

	switch (code) {
	case LV_EVENT_CLICKED:
	{
		lv_obj_clear_flag(guider_ui.screen_ddlist_1, LV_OBJ_FLAG_HIDDEN);
		break;
	}
	default:
		break;
	}
}
static void screen_ddlist_1_event_handler (lv_event_t *e)
{
	lv_event_code_t code = lv_event_get_code(e);

	switch (code) {
	case LV_EVENT_VALUE_CHANGED:
	{
		lv_obj_add_flag(guider_ui.screen_ddlist_1, LV_OBJ_FLAG_HIDDEN);
		uint16_t id = lv_dropdown_get_selected(guider_ui.screen_ddlist_1);
		switch(id) {
		default:
			break;
		}
		break;
	}
	default:
		break;
	}
}
static void screen_btn_4_event_handler (lv_event_t *e)
{
	lv_event_code_t code = lv_event_get_code(e);

	switch (code) {
	case LV_EVENT_CLICKED:
	{
		ui_load_scr_animation(&guider_ui, &guider_ui.screen_6, guider_ui.screen_6_del, &guider_ui.screen_del, setup_scr_screen_6, LV_SCR_LOAD_ANIM_OVER_LEFT, 200, 10, true, true);
		break;
	}
	default:
		break;
	}
}
static void screen_btn_5_event_handler (lv_event_t *e)
{
	lv_event_code_t code = lv_event_get_code(e);

	switch (code) {
	case LV_EVENT_CLICKED:
	{
		ui_load_scr_animation(&guider_ui, &guider_ui.screen_2, guider_ui.screen_2_del, &guider_ui.screen_del, setup_scr_screen_2, LV_SCR_LOAD_ANIM_OVER_LEFT, 200, 10, true, true);
		break;
	}
	default:
		break;
	}
}
static void screen_btn_6_event_handler (lv_event_t *e)
{
	lv_event_code_t code = lv_event_get_code(e);

	switch (code) {
	case LV_EVENT_CLICKED:
	{
		ui_load_scr_animation(&guider_ui, &guider_ui.screen_3, guider_ui.screen_3_del, &guider_ui.screen_del, setup_scr_screen_3, LV_SCR_LOAD_ANIM_OVER_LEFT, 200, 10, true, true);
		break;
	}
	default:
		break;
	}
}
static void screen_btn_7_event_handler (lv_event_t *e)
{
	lv_event_code_t code = lv_event_get_code(e);

	switch (code) {
	case LV_EVENT_CLICKED:
	{
		ui_load_scr_animation(&guider_ui, &guider_ui.screen_4, guider_ui.screen_4_del, &guider_ui.screen_del, setup_scr_screen_4, LV_SCR_LOAD_ANIM_OVER_LEFT, 200, 10, true, true);
		break;
	}
	default:
		break;
	}
}
static void screen_btn_8_event_handler (lv_event_t *e)
{
	lv_event_code_t code = lv_event_get_code(e);

	switch (code) {
	case LV_EVENT_CLICKED:
	{
		ui_load_scr_animation(&guider_ui, &guider_ui.screen_5, guider_ui.screen_5_del, &guider_ui.screen_del, setup_scr_screen_5, LV_SCR_LOAD_ANIM_OVER_LEFT, 200, 10, true, true);
		break;
	}
	default:
		break;
	}
}
void events_init_screen(lv_ui *ui)
{
	lv_obj_add_event_cb(ui->screen_btn_2, screen_btn_2_event_handler, LV_EVENT_ALL, ui);
	lv_obj_add_event_cb(ui->screen_btn_3, screen_btn_3_event_handler, LV_EVENT_ALL, ui);
	lv_obj_add_event_cb(ui->screen_ddlist_1, screen_ddlist_1_event_handler, LV_EVENT_ALL, ui);
	lv_obj_add_event_cb(ui->screen_btn_4, screen_btn_4_event_handler, LV_EVENT_ALL, ui);
	lv_obj_add_event_cb(ui->screen_btn_5, screen_btn_5_event_handler, LV_EVENT_ALL, ui);
	lv_obj_add_event_cb(ui->screen_btn_6, screen_btn_6_event_handler, LV_EVENT_ALL, ui);
	lv_obj_add_event_cb(ui->screen_btn_7, screen_btn_7_event_handler, LV_EVENT_ALL, ui);
	lv_obj_add_event_cb(ui->screen_btn_8, screen_btn_8_event_handler, LV_EVENT_ALL, ui);
}
static void screen_1_btn_3_event_handler (lv_event_t *e)
{
	lv_event_code_t code = lv_event_get_code(e);

	switch (code) {
	case LV_EVENT_CLICKED:
	{
		ui_load_scr_animation(&guider_ui, &guider_ui.screen, guider_ui.screen_del, &guider_ui.screen_1_del, setup_scr_screen, LV_SCR_LOAD_ANIM_FADE_ON, 200, 10, true, true);
		break;
	}
	default:
		break;
	}
}
static void screen_1_slider_1_event_handler (lv_event_t *e)
{
	lv_event_code_t code = lv_event_get_code(e);

	switch (code) {
	case LV_EVENT_VALUE_CHANGED:
	{
		static int32_t cnt = 50;                     // 静态计数值
	lv_event_code_t code = lv_event_get_code(e); // 获取按键值，判断是什么事件
	
	if (code == LV_EVENT_VALUE_CHANGED)                             // 滑动条值改变     
	  cnt = lv_slider_get_value(e->target);                       // 获取滑动条值
	
	lv_label_set_text_fmt(guider_ui.screen_1_label_1, "%d%%", cnt); // 设置文本标签
	
	if(cnt>80)
	{
	  lv_obj_clear_flag(guider_ui.screen_1_msgbox_1, LV_OBJ_FLAG_HIDDEN);
	}
		break;
	}
	default:
		break;
	}
}
void events_init_screen_1(lv_ui *ui)
{
	lv_obj_add_event_cb(ui->screen_1_btn_3, screen_1_btn_3_event_handler, LV_EVENT_ALL, ui);
	lv_obj_add_event_cb(ui->screen_1_slider_1, screen_1_slider_1_event_handler, LV_EVENT_ALL, ui);
}
static void screen_2_btn_3_event_handler (lv_event_t *e)
{
	lv_event_code_t code = lv_event_get_code(e);

	switch (code) {
	case LV_EVENT_CLICKED:
	{
		ui_load_scr_animation(&guider_ui, &guider_ui.screen, guider_ui.screen_del, &guider_ui.screen_2_del, setup_scr_screen, LV_SCR_LOAD_ANIM_FADE_ON, 200, 10, true, true);
		break;
	}
	default:
		break;
	}
}
static void screen_2_list_1_item0_event_handler (lv_event_t *e)
{
	lv_event_code_t code = lv_event_get_code(e);

	switch (code) {
	case LV_EVENT_PRESSED:
	{
		lv_obj_clear_flag(guider_ui.screen_2_cont_3, LV_OBJ_FLAG_HIDDEN);
		break;
	}
	default:
		break;
	}
}
static void screen_2_btn_5_event_handler (lv_event_t *e)
{
	lv_event_code_t code = lv_event_get_code(e);

	switch (code) {
	case LV_EVENT_CLICKED:
	{
		lv_obj_add_flag(guider_ui.screen_2_cont_3, LV_OBJ_FLAG_HIDDEN);
		break;
	}
	default:
		break;
	}
}
void events_init_screen_2(lv_ui *ui)
{
	lv_obj_add_event_cb(ui->screen_2_btn_3, screen_2_btn_3_event_handler, LV_EVENT_ALL, ui);
	lv_obj_add_event_cb(ui->screen_2_list_1_item0, screen_2_list_1_item0_event_handler, LV_EVENT_ALL, ui);
	lv_obj_add_event_cb(ui->screen_2_btn_5, screen_2_btn_5_event_handler, LV_EVENT_ALL, ui);
}
static void screen_3_btn_3_event_handler (lv_event_t *e)
{
	lv_event_code_t code = lv_event_get_code(e);

	switch (code) {
	case LV_EVENT_CLICKED:
	{
		ui_load_scr_animation(&guider_ui, &guider_ui.screen, guider_ui.screen_del, &guider_ui.screen_3_del, setup_scr_screen, LV_SCR_LOAD_ANIM_FADE_ON, 200, 10, true, true);
		break;
	}
	default:
		break;
	}
}
static void screen_3_sw_1_event_handler (lv_event_t *e)
{
	lv_event_code_t code = lv_event_get_code(e);

	switch (code) {
	case LV_EVENT_SHORT_CLICKED:
	{
		lv_obj_t * status_obj = lv_event_get_target(e);
		int status = lv_obj_has_state(status_obj, LV_STATE_CHECKED) ? 1 : 0;
		switch(status) {
		case 0:
		{
			lv_timer_del(task1);
		lv_led_set_brightness(guider_ui.screen_3_led_1,0);
			break;
		}
		case 1:
		{
			//lv_led_set_brightness(guider_ui.screen_3_led_1,255);
		task1 = lv_timer_create(task1_cb, 500, 0);
			break;
		}
		default:
			break;
		}
		break;
	}
	default:
		break;
	}
}
static void screen_3_sw_2_event_handler (lv_event_t *e)
{
	lv_event_code_t code = lv_event_get_code(e);

	switch (code) {
	case LV_EVENT_SHORT_CLICKED:
	{
		lv_obj_t * status_obj = lv_event_get_target(e);
		int status = lv_obj_has_state(status_obj, LV_STATE_CHECKED) ? 1 : 0;
		switch(status) {
		case 0:
		{
			lv_led_set_brightness(guider_ui.screen_3_led_2,0);
			break;
		}
		case 1:
		{
			lv_led_set_brightness(guider_ui.screen_3_led_2,255);
			break;
		}
		default:
			break;
		}
		break;
	}
	default:
		break;
	}
}
static void screen_3_sw_3_event_handler (lv_event_t *e)
{
	lv_event_code_t code = lv_event_get_code(e);

	switch (code) {
	case LV_EVENT_SHORT_CLICKED:
	{
		lv_obj_t * status_obj = lv_event_get_target(e);
		int status = lv_obj_has_state(status_obj, LV_STATE_CHECKED) ? 1 : 0;
		switch(status) {
		case 0:
		{
			lv_led_set_brightness(guider_ui.screen_3_led_3,0);
			break;
		}
		case 1:
		{
			lv_led_set_brightness(guider_ui.screen_3_led_3,255);
			break;
		}
		default:
			break;
		}
		break;
	}
	default:
		break;
	}
}
void events_init_screen_3(lv_ui *ui)
{
	lv_obj_add_event_cb(ui->screen_3_btn_3, screen_3_btn_3_event_handler, LV_EVENT_ALL, ui);
	lv_obj_add_event_cb(ui->screen_3_sw_1, screen_3_sw_1_event_handler, LV_EVENT_ALL, ui);
	lv_obj_add_event_cb(ui->screen_3_sw_2, screen_3_sw_2_event_handler, LV_EVENT_ALL, ui);
	lv_obj_add_event_cb(ui->screen_3_sw_3, screen_3_sw_3_event_handler, LV_EVENT_ALL, ui);
}
static void screen_4_btn_3_event_handler (lv_event_t *e)
{
	lv_event_code_t code = lv_event_get_code(e);

	switch (code) {
	case LV_EVENT_CLICKED:
	{
		ui_load_scr_animation(&guider_ui, &guider_ui.screen, guider_ui.screen_del, &guider_ui.screen_4_del, setup_scr_screen, LV_SCR_LOAD_ANIM_FADE_ON, 200, 10, true, true);
		break;
	}
	default:
		break;
	}
}
static void screen_4_slider_1_event_handler (lv_event_t *e)
{
	lv_event_code_t code = lv_event_get_code(e);

	switch (code) {
	case LV_EVENT_VALUE_CHANGED:
	{
		slider_value=lv_slider_get_value(guider_ui.screen_4_slider_1);
	lv_bar_set_value(guider_ui.screen_4_bar_1,slider_value,LV_ANIM_ON);
	lv_textprogress_set_value(guider_ui.screen_4_textprogress_1,slider_value);
		break;
	}
	default:
		break;
	}
}
void events_init_screen_4(lv_ui *ui)
{
	lv_obj_add_event_cb(ui->screen_4_btn_3, screen_4_btn_3_event_handler, LV_EVENT_ALL, ui);
	lv_obj_add_event_cb(ui->screen_4_slider_1, screen_4_slider_1_event_handler, LV_EVENT_ALL, ui);
}
static void screen_5_btn_3_event_handler (lv_event_t *e)
{
	lv_event_code_t code = lv_event_get_code(e);

	switch (code) {
	case LV_EVENT_CLICKED:
	{
		ui_load_scr_animation(&guider_ui, &guider_ui.screen, guider_ui.screen_del, &guider_ui.screen_5_del, setup_scr_screen, LV_SCR_LOAD_ANIM_FADE_ON, 200, 10, true, true);
		break;
	}
	default:
		break;
	}
}
void events_init_screen_5(lv_ui *ui)
{
	lv_obj_add_event_cb(ui->screen_5_btn_3, screen_5_btn_3_event_handler, LV_EVENT_ALL, ui);
}
static void screen_6_btn_3_event_handler (lv_event_t *e)
{
	lv_event_code_t code = lv_event_get_code(e);

	switch (code) {
	case LV_EVENT_CLICKED:
	{
		ui_load_scr_animation(&guider_ui, &guider_ui.screen, guider_ui.screen_del, &guider_ui.screen_6_del, setup_scr_screen, LV_SCR_LOAD_ANIM_FADE_ON, 200, 10, true, true);
		break;
	}
	default:
		break;
	}
}
static void screen_6_list_1_item2_event_handler (lv_event_t *e)
{
	lv_event_code_t code = lv_event_get_code(e);

	switch (code) {
	case LV_EVENT_PRESSED:
	{
		lv_obj_clear_flag(guider_ui.screen_6_msgbox_1, LV_OBJ_FLAG_HIDDEN);
		break;
	}
	default:
		break;
	}
}
void events_init_screen_6(lv_ui *ui)
{
	lv_obj_add_event_cb(ui->screen_6_btn_3, screen_6_btn_3_event_handler, LV_EVENT_ALL, ui);
	lv_obj_add_event_cb(ui->screen_6_list_1_item2, screen_6_list_1_item2_event_handler, LV_EVENT_ALL, ui);
}

void events_init(lv_ui *ui)
{

}
